import React, { useState } from 'react';
import { Button, Drawer, message, Tree } from 'antd';
import { authApi } from "../apis/roleApi"
const treeData = [
    {
        title: '首页',
        key: '/home/',
    },
    {
        title: '用户管理',
        key: '/home/user',
    },
    {
        title: '角色管理',
        key: '/home/role',
    },
    {
        title: '店铺管理',
        key: '/home/shop',
    },
    {
        title: '商品管理',
        key: '/home/product',
        children: [
            {
                title: '商品列表',
                key: '',
                children: [
                    {
                        title: '商品查询',
                        key: '/home/list',
                    },
                    {
                        title: '商品添加',
                        key: '/home/productAdd',
                    },
                    {
                        title: '商品修改',
                        key: '/home/productUpdate',
                    }
                ]
            },
            {
                title: '商品分类',
                key: '/home/category',
            }
        ]
    },
    {
        title: '财务管理',
        key: '/home/chart',
        children: [
            {
                title: '工资数据',
                key: '/home/salary',
            },
            {
                title: '销售数据',
                key: '/home/sale',
            }
        ]
    },
];
const MyDrawer = ({ close, chooseRole }) => {
    const [chooseMenus, setChooseMenus] = useState([])
    const onClose = () => {
        close(false)
    };
    const onCheck = (checkedKeys, info) => {
        console.log(checkedKeys);
        setChooseMenus(checkedKeys)
    };
    const addAuth = async () => {
        let obj = {
            id: chooseRole._id,
            authTime: new Date(),
            authUser: "xiaowang",
            menus: chooseMenus
        }
        const res = await authApi(obj)
        if (res.data.code) {
            message.success("授权成功")
        } else {
            message.error("授权失败")
        }

    }
    return (
        <>
            <Drawer title="授权" placement="right" onClose={onClose} open={true}>
                <Tree
                    checkable
                    defaultExpandedKeys={['/home/product', '/home/chart']}
                    defaultCheckedKeys={chooseRole.menus}
                    onCheck={onCheck}
                    treeData={treeData}
                />
                <Button onClick={addAuth}>授权</Button>
            </Drawer>
        </>
    );
};
export default MyDrawer;